package com.share.device.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.utils.StringUtils;
import com.share.device.domain.Cabinet;
import com.share.device.domain.Station;
import com.share.device.domain.StationLocation;
import com.share.device.mapper.StationMapper;
import com.share.device.repository.StationLocationRepository;
import com.share.device.service.ICabinetService;
import com.share.device.service.IRegionService;
import com.share.device.service.IStationService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;


@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private ICabinetService cabinetService;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private StationLocationRepository stationLocationRepository;


    @Override
    public List<Station> selectStationList(Station station) {
        // 1.查出站点列表
        List<Station> stations = stationMapper.selectStationList(station);
        // 2.获取站点列表中的柜机id集合
        Stream<Long> cabinetIds = stations.stream().map(Station::getCabinetId);
        // 3.构建柜机id和柜机编号的map
        HashMap<Long, String> cabinetIdAndNoMap = new HashMap<>();
        cabinetIds.forEach(cabinetId -> {
            if (cabinetId != null) {
                Cabinet cabinet = cabinetService.getById(cabinetId);
                if (cabinet != null) {
                    cabinetIdAndNoMap.put(cabinetId, cabinet.getCabinetNo());
                }
            }
        });
        // 4.给站点列表中的柜机编号赋值
        for (Station station1 : stations) {
            station1.setCabinetNo(cabinetIdAndNoMap.get(station1.getCabinetId()));
        }

        return stations;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveStation(Station station) {
        String provinceName = regionService.getNameByCode(station.getProvinceCode());
        String cityName  = regionService.getNameByCode(station.getCityCode());
        String districtName = regionService.getNameByCode(station.getDistrictCode());

        station.setFullAddress(provinceName + cityName + districtName + station.getAddress());
        this.save(station);
        
        // 同步站点信息到mongoDB
        StationLocation stationLocation = new StationLocation();
        stationLocation.setId(ObjectId.get().toString());
        stationLocation.setStationId(station.getId());
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
        stationLocation.setCreateTime(new Date());
        stationLocationRepository.save(stationLocation);


        return 1;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStation(Station station) {
        String provinceName = regionService.getNameByCode(station.getProvinceCode());
        String cityName  = regionService.getNameByCode(station.getCityCode());
        String districtName = regionService.getNameByCode(station.getDistrictCode());

        station.setFullAddress(provinceName + cityName + districtName + station.getAddress());
        this.updateById(station);

        // 同步站点信息到mongoDB
        StationLocation stationLocation = new StationLocation();
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
        stationLocationRepository.save(stationLocation);

        return 1;
    }

    @Override
    public int setData(Station station) {
        this.updateById(station);

        // 更新柜机
        Cabinet cabinet = cabinetService.getById(station.getCabinetId());
        cabinet.setStatus("1");
        cabinetService.updateById(cabinet);

        return 1;
    }

    @Override
    public void updateData() {
        List<Station> list = this.list();
        for (Station station : list) {
            StationLocation stationLocation = stationLocationRepository.getByStationId(station.getId());
            if (stationLocation == null) {
                stationLocation = new StationLocation();
                stationLocation.setId(ObjectId.get().toString());
                stationLocation.setStationId(station.getId());
                stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
                stationLocation.setCreateTime(new Date());
                stationLocationRepository.save(stationLocation);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeByIds(Collection<?> list) {
        for (Object id : list) {
            stationLocationRepository.deleteByStationId(Long.parseLong(id.toString()));
        }
        return super.removeByIds(list);
    }
}
